package com.trkj.service.wxy;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.DefinitionDeptDao_wxy;
import com.trkj.dao.wxy.ExamArrangeDao;
import com.trkj.dao.wxy.ExamDao;
import com.trkj.dao.wxy.ExamGradeDao;
import com.trkj.dao.wxy.QuestionBankDao;
import com.trkj.pojo.Admission;
import com.trkj.pojo.ExamArrange;
import com.trkj.pojo.ExamGrade;
import com.trkj.pojo.ExamPaper;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.ExamGradePageVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExamGradeService {
	@Autowired
	private ExamDao examDao;
	@Autowired
	private ExamArrangeDao examArrangeDao;
	@Autowired
	private QuestionBankDao questionBankDao;
	@Autowired
	private DefinitionDeptDao_wxy definitionDeptDao;
	@Autowired
	private ExamGradeDao examGradeDao;
	
	/**
	 * 查询所有的记录分页
	 */
	public List<ExamGradePageVo> queryByExamArrangeQuestion(int page,int rows){
		List<ExamGrade> list = examGradeDao.queryByExamArrange(new PageRequest(page-1, rows));
		List<ExamGradePageVo> listExamArrangePageVo = new ArrayList<>();
		if(list != null){
			for(ExamGrade objs: list){
				ExamGradePageVo examGradePageVo = new ExamGradePageVo();
				if(objs.getExamArrange()!=null){
					ExamArrange r =  objs.getExamArrange();
					examGradePageVo.setArrangeName(r.getArrangeName());
				}
				ExamPaper e = objs.getExamArrange().getExamPapers().get(0);
				examGradePageVo.setClassSjName(e.getClassSjName());
				examGradePageVo.setEpStarttime(e.getEpStarttime());
				examGradePageVo.setEpEndtime(e.getEpEndtime());
				examGradePageVo.setEpDefen(e.getEpDefen());
				examGradePageVo.setEpShichang(e.getEpShichang());
				
				ExamGrade g = objs.getExamArrange().getExamGrades().get(0);
				examGradePageVo.setGrId(g.getGrId());
				examGradePageVo.setGrDate(new Timestamp(new Date().getTime()));
				examGradePageVo.setGrState(g.getGrState());
				examGradePageVo.setGrPerson(g.getGrPerson());
				
				Admission a = objs.getAdmission();
				examGradePageVo.setAdmId(a.getAdmId());
				
				StaffInformation s = objs.getAdmission().getStaffInformation();
				examGradePageVo.setStainName(s.getStainName());
				
				
				listExamArrangePageVo.add(examGradePageVo);
			}
		}
		return listExamArrangePageVo;
		
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return examGradeDao.count();
		
	}
	
	/**
	 * 删除
	 * @param grId
	 * @throws ServiceException
	 */
	public void kjpf_delete(Long grId) throws ServiceException{
		try {
			examGradeDao.delete(grId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
}
